package UserProject

import (
	"autoDeploy/service/project"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/golang-jwt/jwt"
	"log"
	"net/http"
)

type UserProject struct{}

// GetProject 根据用户id返回用户的项目信息给前端
func (p UserProject) GetProject(c *gin.Context) {
	claims, _ := c.Get("claims")
	if claims != nil {
		claimsMap := claims.(jwt.MapClaims)
		//获取用户id
		UserIDFloat := claimsMap["user_id"].(float64)
		UserID := uint64(int64(UserIDFloat))
		//调用service层方法，获取用户的项目详细信息
		GitProDetails, err := project.GetUserGitInfos(UserID)
		if err != nil {
			c.JSON(200, gin.H{"code": 0, "user_id": UserID, "git_pro_details": GitProDetails, "message": "根据用户id查询项目git信息失败" + err.Error()})
			return
		}
		c.JSON(200, gin.H{"code": 1, "user_id": UserID, "git_pro_details": GitProDetails, "message": "根据用户id查询项目git信息成功"})
		return
	}
}

// Deploy 根据用户的git地址部署
func (p UserProject) Deploy(c *gin.Context) {
	claims, _ := c.Get("claims")
	if claims != nil {
		claimsMap := claims.(jwt.MapClaims)
		//获取用户id
		UserIDFloat := claimsMap["user_id"].(float64)
		UserID := uint64(int64(UserIDFloat))

		// 定义一个结构体来接收git地址
		var deployRequest struct {
			GitAddress string `json:"git_address" binding:"required"`
		}

		// 从请求体中绑定git地址
		if err := c.ShouldBindJSON(&deployRequest); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}
		//调用service层方法，进行部署
		code, err := project.DeployProject(UserID, deployRequest.GitAddress)
		if err != nil {
			c.JSON(http.StatusOK, gin.H{"code": code, "message": err.Error()})
			return
		}
		c.JSON(http.StatusOK, gin.H{"code": code, "message": "部署成功"})
	}
}

// WebHook 接收git提交的信息
func (p UserProject) WebHook(c *gin.Context) {
	// 获取事件类型
	eventType := c.GetHeader("X-Gitee-Event")

	switch eventType {
	case "Push Hook":
		// 定义变量来存储解析后的数据
		var pusher project.Pusher
		var projectDetails project.ProjectDetails

		// 使用 ShouldBindBodyWith 方法绑定数据 推送人
		if err := c.ShouldBindBodyWith(&pusher, binding.JSON); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}
		//fmt.Println("Pusher绑定成功", pusher)

		// 再次使用 ShouldBindBodyWith 方法绑定 项目详情
		if err := c.ShouldBindBodyWith(&projectDetails, binding.JSON); err != nil {
			log.Printf("Failed to bind ProjectDetails: %s", err.Error())
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}
		//fmt.Println("项目绑定成功")
		//fmt.Println("Handling Push")
		c.JSON(http.StatusOK, gin.H{"pusher": pusher, "projectDetails": projectDetails})
		//调用service层的方法进行 Push 事件的处理
		code, err := project.SavePushMsg(projectDetails, pusher)
		if err != nil {
			c.JSON(http.StatusOK, gin.H{"code": code, "message": err.Error()})
		}

	case "Tag Push Hook":
		var TagEvent project.ProjectTag
		if err := c.ShouldBindJSON(&TagEvent); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}
		c.JSON(http.StatusOK, gin.H{"TagEvent": TagEvent})
		//调用service层的方法进行 Tag 部署和插入处理
		code, err := project.TagDeploy(TagEvent)
		if err != nil {
			fmt.Errorf("调用service层的方法进行 Tag 部署和插入处理:%s", err.Error())
			c.JSON(http.StatusOK, gin.H{"code": code, "message": err.Error()})
		}

	default:
		c.JSON(http.StatusBadRequest, gin.H{"error": "Unsupported event type"})
		return
	}

}

// GetPort 查看用户的项目端口
func (p UserProject) GetPort(c *gin.Context) {
	gitAddress := c.Query("git_address")
	//调用service层的方法
	url, err := project.GetIPAndPort(gitAddress)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 0, "url": url, "message": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 1, "url": url, "message": "查看端口成功"})
	return
}

// GetProjectSum 获取项目数量
func (p UserProject) GetProjectSum(c *gin.Context) {
	claims, _ := c.Get("claims")
	if claims != nil {
		claimsMap := claims.(jwt.MapClaims)
		//获取用户id
		UserIDFloat := claimsMap["user_id"].(float64)
		UserID := uint64(int64(UserIDFloat))

		//调用service层方法，进行获取数据
		sum, err := project.GetProjectSum(UserID)
		if err != nil {
			c.JSON(http.StatusOK, gin.H{"code": 0, "sum": 0, "message": err.Error()})
			return
		}
		c.JSON(http.StatusOK, gin.H{"code": 1, "sum": sum, "message": "查询成功"})
		return
	}
}

// GetMemberSum 根据用户id获取成员总数
func (p UserProject) GetMemberSum(c *gin.Context) {
	claims, _ := c.Get("claims")
	if claims != nil {
		claimsMap := claims.(jwt.MapClaims)
		UserIDFloat := claimsMap["user_id"].(float64)
		UserID := uint64(int64(UserIDFloat))
		sum, err := project.GetMemberSum(UserID)
		if err != nil {
			c.JSON(http.StatusOK, gin.H{"code": 0, "sum": 0, "message": err.Error()})
			return
		}
		c.JSON(http.StatusOK, gin.H{"code": 0, "sum": sum, "message": "查询成功"})
		return
	}
}
